#include "stdafx.h"
#include "CameraObject.h"

CameraObject::CameraObject() {
}

CameraObject::~CameraObject() {
}

void CameraObject::createMovableObject(Ogre::SceneManager* sceneMgr, unsigned int id) {
}

void CameraObject::setSceneNode(Ogre::SceneNode *node) {
	m_node = node;
	m_yawNode = m_node->createChildSceneNode();
	m_pitchNode = m_yawNode->createChildSceneNode();
	m_rollNode = m_pitchNode->createChildSceneNode();
}

Ogre::SceneNode* CameraObject::getCameraNode() {
	return m_rollNode;
}	

void CameraObject::updateLogic() {
}

void CameraObject::turn(float amount) {
	if(m_node) {
		m_node->yaw(Ogre::Degree(amount));
	}
}

void CameraObject::move(float x, float y, float z) {
	Ogre::Vector3 v3 = Ogre::Vector3(x, y, z);
	m_node->translate(m_yawNode->getOrientation() * m_pitchNode->getOrientation() * m_rollNode->getOrientation() * v3, Ogre::SceneNode::TS_LOCAL);
}

void CameraObject::cameraLookAround(float mouseRotX, float mouseRotY) {
	if(m_node) {
		Ogre::Real pitchAngle;
		Ogre::Real pitchAngleSign;

		// Yaws the camera according to the mouse relative movement.
		m_yawNode->yaw(Ogre::Degree(mouseRotX));

		// Pitches the camera according to the mouse relative movement.
		m_pitchNode->pitch(Ogre::Degree(mouseRotY));

		// Angle of rotation around the X-axis.
		pitchAngle = ( 2 * Ogre::Degree( Ogre::Math::ACos(m_pitchNode->getOrientation().w) ).valueDegrees() );

		// Just to determine the sign of the angle we pick up above, the
		// value itself does not interest us.
		pitchAngleSign = m_pitchNode->getOrientation().x;

		// Limit the pitch between -90 degress and +90 degrees, Quake3-style.
		if (pitchAngle > 90.0f) {
			if (pitchAngleSign > 0) {
				// Set orientation to 90 degrees on X-axis.
				m_pitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f),
																Ogre::Math::Sqrt(0.5f), 0, 0));
			}
			else if (pitchAngleSign < 0) {
				// Sets orientation to -90 degrees on X-axis.
				m_pitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f),
																-Ogre::Math::Sqrt(0.5f), 0, 0));
			}
		}
	}
}